//
//  PRTLevel.m
//  Pirates
//
//  Created by goofara on 4/4/14.
//  Copyright (c) 2014 Training Camp. All rights reserved.
//

#import "PRTLevel.h"

@implementation PRTLevel

-(id)init
{
    return [self initWithTiledMap:nil];
}

-(id)initWithTiledMap:(NSString *)tiledMap {
    self = [super init];
    if(self)
    {
        self.tiledMap = [CCTiledMap tiledMapWithFile:tiledMap];
        self.collisionLayer = [self.tiledMap layerNamed:@"Collision"];
        [self.collisionLayer setVisible:NO];
        
    }
    return self;
}

-(CGPoint) tileCoordinateForPosition:(CGPoint) position
{
    

    float x = ([CCDirector sharedDirector].contentScaleFactor * -position.x /* +(screenBounds.size.width*0.5f)*/) / self.tiledMap.tileSize.width;
    float tileHeight = self.tiledMap.tileSize.height/ [CCDirector sharedDirector].contentScaleFactor;
    float y = ((((self.tiledMap.mapSize.height * tileHeight) + position.y) /*- (screenBounds.size.height * 0.5f)*/) / tileHeight);
    
    return CGPointMake((int)(x), (int)(y));
}

-(BOOL)canMoveToTileCoordinateForPosition:(CGPoint)position
{
    
    CGPoint tileCoordinate = [self tileCoordinateForPosition:position];
    
    NSUInteger tileGID = [self.collisionLayer tileGIDAt:tileCoordinate];
    
    if(tileGID)
    {
        NSDictionary * properties = [self.tiledMap propertiesForGID:tileGID];
        if(properties)
        {
            
            NSString * collision = (NSString *)[properties valueForKey:@"collidable"];
            if([collision isEqualToString:@"YES"])
            {
                
                return NO;
            }
            
        }
        
    }
    
    return YES;
}


@end
